//
//  TagViewController.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "TagViewController.h"
#import "MainDataHelper.h"
#import "UIImageView+WebCache.h"
#import "TagModel.h"
#import "TagCollectionViewCell.h"
#import "TagDetailViewController.h"

@interface TagViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, copy) NSString *string;
@property (nonatomic, copy) NSString *bodyString;

@end

@implementation TagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
    
    [self loadCollectionView];
}

#pragma mark --- 数据解析部分 ---
- (void)loadData
{
    self.dataArray = [NSMutableArray array];
    
    self.string = @"http://api2.pianke.me/timeline/tags";
    self.bodyString = @"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=10&start=0&version=3.0.6";
    
    [MainDataHelper postDataWithUrlString:self.string andBodyString:self.bodyString compare:^(id object) {
        NSDictionary *dic = object;
        NSArray *array = dic[@"data"][@"list"];
        
        for (NSDictionary *dic1 in array) {
            TagModel *model = [[TagModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        [self.collectionView reloadData];
    }];
}

#pragma mark --- collectionView部分 ---
- (void)loadCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 40)/3, (self.view.frame.size.width - 40)/3);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height - 20) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (TagCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   [self.collectionView registerClass:[TagCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
    
    TagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
     TagModel *model = self.dataArray[indexPath.row];
    [cell.titleImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    cell.titleLabel.text = model.tag;
    
    return cell;
}

#pragma mark --- 点击跳转 ---
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    TagModel *model = self.dataArray[indexPath.row];
    TagDetailViewController *tagDetailVC = [[TagDetailViewController alloc] init];
    tagDetailVC.tag = model.tag;
    [self.navigationController pushViewController:tagDetailVC animated:YES];
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
